Create a JavaScript program to test whether the first character of a string is uppercase or not.
Example:
upper_case('Abcd'); → First character is uppercase
upper_case('abcd'); → First character is not uppercase
function myFunc1(str) {
return /^[A-Z]/.test(str)
? "First character is uppercase"
: "First character is not uppercase";
}
document.getElementById("ex1").innerText = myFunc1("Abcd");
Create a function to check valid Visa Credit Card number.
Visa card starts with 4, length 14 or 16 digits.
Example:
is_creditCard("378282246310006") → Invalid Visa Card
is_creditCard("44587210236599") → Valid Visa Card
function myFunc2(str) {
return /^4\d{13}(\d{2})?$/.test(str)
? "Valid Visa Card"
: "Invalid Visa Card";
}
document.getElementById("ex2").innerText = myFunc2("44587210236599");
Create a function to replace date format and return new string.
Example:
change_str("Albert Einstein was born on 14/03/1879")
Output → Albert Einstein was born in year of 1879 on 14th March
function myFunc3(str) {
return str.replace(
/(\d{2})\/(\d{2})\/(\d{4})/,
(_, d, m, y) => {
const months = [
"January","February","March","April","May","June",
"July","August","September","October","November","December"
];
const day = d.replace(/^0/, "");
const suffix =
day == 1 || day == 21 || day == 31 ? "st" :
day == 2 || day == 22 ? "nd" :
day == 3 || day == 23 ? "rd" : "th";
return `in year of ${y} on ${day}${suffix} ${months[+m - 1]}`;
}
);
}
document.getElementById("ex3").innerText =
myFunc3("Albert Einstein was born on 14/03/1879");
cument.getElementById("ex3").innerText = myFunc3("Hello", "Coders");
Write a JavaScript program to create custom Trim Function.
Example:
trim(" Dcoders ") → Dcoders
trim("Himatnagar ") → Himatnagar
function myFunc4(str) {
return str.replace(/^\s+|\s+$/g, "");
}
document.getElementById("ex4").innerText =
myFunc4(" Dcoders ");
Write a JavaScript program to check entered Aadhaar Card number is valid or not.
(Number length must be exactly 12)
Example:
is_aadhar(6541203689) → False
is_aadhar(457675164555) → True
function myFunc5(str) {
return /^\d{12}$/.test(str);
}
document.getElementById("ex5").innerText =
myFunc5("457675164555");
Write a JavaScript program to count the words by ignoring special characters or symbols.
Example:
count("India%is*my@country") → 4
count("I%love*my@country#very&much") → 6
function myFunc6(str) {
const words = str.replace(/[^a-zA-Z]+/g, " ").trim();
return words ? words.split(/\s+/).length : 0;
}
document.getElementById("ex6").innerText =
myFunc6("I%love*my@country#very&much");
Write a JavaScript function to count the number of vowels in a given string.
Vowels in English: A, E, I, O, U.
Example:
count_vowels("A Python") → 2
count_vowels("The javascript") → 4
count_vowels("The quick brown fox jumps over the lazy dog") → 11
function myFunc7(str) {
const matches = str.match(/[aeiou]/gi);
return matches ? matches.length : 0;
}
document.getElementById("ex7").innerText =
myFunc7("The quick brown fox jumps over the lazy dog");
Write a JavaScript function to check domain name is secured or not.
Return domain name if it is secured (1st character in Capital Case).
Example:
check_domain("https://www.google.com") → Google
check_domain("http://www.my-javascript.in") → Insecure domain
check_domain("https://www.emaad.com") → Emaad
function myFunc8(str) {
const match = str.match(/^https:\/\/(?:www\.)?([^\.]+)/i);
if (!match) return "Insecure domain";
const name = match[1];
return name.charAt(0).toUpperCase() + name.slice(1);
}
document.getElementById("ex8").innerText =
myFunc8("https://www.google.com");